import React from 'react';
import { useNavigate } from "react-router-dom";
import './LogoutButton.css'; // импорт стилей

const LogoutButton = () => {
    const navigate = useNavigate();
    const handleLogout = async () => {
        const response = await fetch('http://localhost:3000/logout', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Authorization': `Bearer ${sessionStorage.getItem('access_token')}`
            },
        });
        if (response.ok) {
            sessionStorage.removeItem('access_token');
            sessionStorage.removeItem('role');
            navigate('/login');
        } else {
            console.log(await response.json());
        }
    };

    return (
        <button onClick={handleLogout} className="logoutButton">Выход</button>
    );
}

export default LogoutButton;
